import {type EventHandler, EventName} from "@/event/event.ts";

import {ElMessage} from 'element-plus'
//全局消息处理器 使用 ElMessage 弹出消息
export class GlobalMessage {
    type: MessageType = MessageType.SUCCESS
    message?: string
    duration: number = 1500;
    showClose: boolean = true;
    center: boolean = true;
    offset: number = 0

    static buildMessage(type: MessageType, message: string): GlobalMessage {
        const messageInfo = new GlobalMessage()
        messageInfo.type = type
        messageInfo.message = message
        return messageInfo
    }

    buildType(type: MessageType): this {
        this.type = type
        return this
    }

    buildMessage(message: string): this {
        this.message = message
        return this
    }

    buildDuration(duration: number): this {
        this.duration = duration
        return this
    }

    buildShowClose(showClose: boolean): this {
        this.showClose = showClose
        return this
    }

    buildCenter(center: boolean): this {
        this.center = center
        return this
    }

    buildOffset(offset: number): this {
        this.offset = offset
        return this
    }
}

export enum MessageType {
    SUCCESS = "success",  // 注意：Element Plus 使用小写
    WARN = "warning",
    ERROR = "error",
    INFO = "info"
}

export class GlobalMessageEventHandler implements  EventHandler{
    support(name: EventName): boolean {
        return name == EventName.GLOBAL_MESSAGE
    }

    handler(name: EventName, param: GlobalMessage) {
        if(this.support(name)){
            ElMessage({
                ...param
            })
        }
    }
}